package com.xuyiwei.mayi.service;

import com.xuyiwei.mayi.entity.User;
import com.xuyiwei.mayi.mapper1.UserMapper1;
import com.xuyiwei.mayi.mapper2.UserMapper2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xuyiwei on 2019/4/19.
 */
@Service
public class UserServiceImpl1 implements UserService1 {
    @Autowired
    private UserMapper1 userMapper1;

    @Autowired
    private UserMapper2 userMapper2;

    @Override
    public List<User> findAll() {
        return userMapper1.findAll();
    }

    @Override
    public User findOne(Integer id) {
        return userMapper1.findOne(id);
    }

    @Override
    public void addOne(User user) {
        userMapper1.addOne(user);
    }

    @Override
    public void updateOne(User user) {
        userMapper1.updateOne(user);
    }

    @Override
    public void delOne(Integer id) {
        userMapper1.delOne(id);
    }

    @Override
    @Transactional
    public void addTx() {
        //springboot如果多数据源操作  默认回滚以第一个事务为准
        //这个事务抛出异常会回滚
        User user1 = new User();
        user1.setName("1-10");
        user1.setAge(110);
        userMapper1.addOne(user1);

        //这个事务抛出异常不会回滚，仍然会保存
        User user2 = new User();
        user2.setName("2-10");
        user2.setAge(210);
        userMapper2.addOne(user2);

        int i = 1/0;

    }
}
